package cn.commander.utils;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;


public class CustomDateSerializer extends JsonSerializer<Date> {

	/**
	 * JSON时间格式化
	 * 
	 * @param date
	 * @param jsonGen
	 * @param provider
	 * @throws IOException
	 * @throws JsonProcessingException
	 * @return
	 */
	@Override
	public void serialize(Date date, JsonGenerator jsonGen, SerializerProvider provider) throws IOException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateStr = sdf.format(date);
		String targetDateStr = dateStr.replace(" ", "T");
		jsonGen.writeString(targetDateStr);
	}

}
